<!doctype html><html><head>
    <title>Canvas drawImage example 3</title>
    <meta name="DC.creator" content="Kamiel Martinet, http://www.martinet.nl/">
    <meta name="DC.publisher" content="Mozilla Developer Center, http://developer.mozilla.org">

<script src="../canvas.vml.js"></script>
<script src="../canvas.js"></script>

<script id="code">
function oncanvasready() {
    drawShape(document.getElementById('vmlcanvas'));
}
function matrix() {
    drawShape(document.getElementById('vmlcanvas'), 1);
}

var globalAlpha = 1.0
function switchAlpha() {
    globalAlpha -= 0.2;
    drawShape(document.getElementById('vmlcanvas'));
}

function drawShape(canvas, matrix){
    if (!canvas.getContext){ return; }

    var ctx = canvas.getContext('2d');

    ctx.xClipColor = "#fff";
    ctx.clear();
    if (matrix) {
        ctx.translate(-10, 5);
        ctx.scale(1.2, 0.8);
        ctx.rotate(5 * Math.PI / 180);
    }
    ctx.globalAlpha = globalAlpha;

    ctx.beginPath();
    ctx.arc(100, 100, 100, 0, Math.PI * 2, 1);
    ctx.clip();

    ctx.strokeStyle = "red";
    ctx.lineWidth = 3;

    ctx.drawImage(document.getElementById('source'),33,71,104,124,21,20,87,104);
    ctx.drawImage(document.getElementById('frame'),0,0);

    ctx.stroke();
}
</script>
    <style type="text/css">
      body { margin: 20px; font-family: arial,verdana,helvetica; background: #fff;}
      h1 { font-size: 140%; font-weight:normal; color: #036; border-bottom: 1px solid #ccc; }
      h2 { font-size: 100%; color: #036; }
      canvas { float: left; margin-right: 20px; margin-bottom: 20px; }
      pre { float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
      code { white-space: pre; float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
    </style>
  </head>
  <body>
    <div>
      <h1>Canvas <code>drawImage</code> example 3</h1>
      <input type="button" value="matrix effect" onclick="matrix()" />
      <input type="button" value="alpha -0.2" onclick="switchAlpha()" />
      <br />
      <canvas id="vmlcanvas" class="vml" width="200" height="200"></canvas>
      <code>
function oncanvasready() {
    drawShape(document.getElementById('vmlcanvas'));
}
function matrix() {
    drawShape(document.getElementById('vmlcanvas'), 1);
}

var globalAlpha = 1.0
function switchAlpha() {
    globalAlpha -= 0.2;
    drawShape(document.getElementById('vmlcanvas'));
}

function drawShape(canvas, matrix){
    if (!canvas.getContext){ return; }

    var ctx = canvas.getContext('2d');

    ctx.xClipColor = "#fff";
    ctx.clear();
    if (matrix) {
        ctx.translate(-10, 5);
        ctx.scale(1.2, 0.8);
        ctx.rotate(5 * Math.PI / 180);
    }
    ctx.globalAlpha = globalAlpha;

    ctx.beginPath();
    ctx.arc(100, 100, 100, 0, Math.PI * 2, 1);
    ctx.clip();

    ctx.strokeStyle = "red";
    ctx.lineWidth = 3;

    ctx.drawImage(document.getElementById('source'),33,71,104,124,21,20,87,104);
    ctx.drawImage(document.getElementById('frame'),0,0);

    ctx.stroke();
}
      </code>
      <div style="float:left;">
        <h2>Source img</h2>
        <img id="source" src="img/rhino.jpg" width="300" height="227" alt="">
        <img id="frame" src="img/picture_frame.png" width="132" height="150" alt="">
      </div>
    </div>
  </body>
</html>
